# -*- coding=utf8 -*-
from django.template import RequestContext

__author__ = 'nickleo'

from django.core.files.storage import default_storage

from django.views.generic.base import TemplateView
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.template.loader import get_template
import manager_utils
import shape_utils, tree_utils, progress_utils


class FakeField(object):
    storage = default_storage


# class HomePageView(TemplateView):
# template_name = 'background.html'
#
# def get_context_data(self, **kwargs):
# context = super(HomePageView, self).get_context_data(**kwargs)
# # messages.info(self.request, 'This is a demo of a message.')
# return context
def home_page(request):
    t = get_template('background.html')
    shape_model_open = False
    if request.user.is_authenticated:
        # username = request.session.get('username')
        action = 'shape'
        if request.GET.get('action') is None:
            action = 'shape'
        else:
            action = request.GET.get('action')
        print action
        # 这里判断,不认识的都跳转到shape
        if action == 'shape':
            actionContext = 'shape.html'
        elif action == 'topic':
            actionContext = 'topic.html'
            actionDest = "Topic 状态查询"
            treeNodes, action_alert = tree_utils.tree()

        else:
            actionContext = 'shape.html'

        if actionContext == 'shape.html':
            shape_model_open = True
            actionDest = "Borker 运行实例监控"
            shapeList, action_alert = shape_utils.all()

        print locals()
        c = RequestContext(request, locals())
        return HttpResponse(t.render(c))
    else:
        c = RequestContext(request, locals())
        return HttpResponse(t.render(c))


def actions(request, action):
    t = get_template('background.html')
    shape_model_open = False
    if request.user.is_authenticated:
        if action == 'shape':
            actionContext = 'shape.html'

        elif action == 'topic':
            actionContext = 'topic.html'
            actionDest = "Topic 状态查询"
            treeNodes, action_alert = tree_utils.tree()
        elif action == 'chart':
            actionContext = 'chart.html'
            actionDest = "运行 数据 "
            cluster_bid_mapping, action_alert = shape_utils.cluster_bid_mapping()
        elif action == 'progress':
            actionContext = 'progress.html'
            actionDest = "消费 积压 数据 "
            consumer_names, action_alert = progress_utils.get_consumer_name()
        elif action == 'manager':
            actionContext = 'manager.html'
            actionDest = "MQ 管理"
            clusters, topics, action_alert = manager_utils.get_clusters_and_topics()
            consumer_names, _ = progress_utils.get_group()
            brokerAddrs,_ = manager_utils.get_broker_addrs()
            clusters_namesvr_mapping,_ = manager_utils.get_broker_clusters()
            brokerCfgText,mqNettyCfgText=manager_utils.get_config()
        elif action == 'message':
            actionContext = 'message.html'
            actionDest = "消息查询"
            clusters, topics, action_alert = manager_utils.get_clusters_and_topics()
        else:
            action = 'shape'
            actionContext = 'shape.html'

        if actionContext == 'shape.html':
            shape_model_open = True
            actionDest = "Borker 运行实例监控"
            shapeList, action_alert = shape_utils.all()

        print locals()
        c = RequestContext(request, locals())
        return HttpResponse(t.render(c))
    else:
        c = RequestContext(request, locals())
        return HttpResponse(t.render(c))


# class LoginPageView(TemplateView):
#     template_name = 'loginFull.html'
#     def get_context_data(self, **kwargs):
#         context = super(LoginPageView, self).get_context_data(**kwargs)
#         # messages.info(self.request, 'This is a demo of a message.')
#         return context

def login_page(request):
    t = get_template('loginFull.html')
    c = RequestContext(request, locals())
    return HttpResponse(t.render(c))


def login_view(request):
    user = authenticate(username=request.POST['username'], password=request.POST['password'])
    username = request.POST['username']
    if user is not None:
        # request.session['username'] = username
        login(request, user)
        return HttpResponseRedirect("/")
    else:
        request.session['username'] = ''
        return home_page(request)


def logout_view(request):
    logout(request)
    return HttpResponseRedirect("/")
